package com.macrounion.atv;

import com.macrounion.base.annotation.EnableWeb;
import jdk.nashorn.internal.ir.ReturnNode;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@SpringBootApplication
@ServletComponentScan
@EnableAutoConfiguration
@EnableSwagger2
@EnableWeb
@EnableScheduling
@EnableCaching
@RestController
public class AtvWebStartUp{

	@Value("${spring.application.name}")
	private String appName;

	private static final Log log = LogFactory.getLog(AtvWebStartUp.class);

	private static String[] args;
	private static ConfigurableApplicationContext context;

	public static void main(String[] args) {
		AtvWebStartUp.args=args;
		AtvWebStartUp.context=SpringApplication.run(AtvWebStartUp.class,args);
	}

	@GetMapping("/restart")
	public void restart(){
		ExecutorService threadPool=new ThreadPoolExecutor(1,1,0, TimeUnit.SECONDS,new ArrayBlockingQueue<>(1),new ThreadPoolExecutor.DiscardOldestPolicy());
		threadPool.execute(()->{
			context.close();
			context=SpringApplication.run(AtvWebStartUp.class,args);
		});
		threadPool.shutdown();
	}
}
